package com.wiegand.content.contents.projectcontent {
	import com.wiegand.event.ClickEvent;
	import com.wiegand.util.ColorRectangle;
	import com.wiegand.util.MovingContainerBox;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author gandy
	 */
	 
	public class ProjectScroller extends Sprite {
		private var _xml : XMLList;
		private var _container : Sprite;
		private var _projects : Array;
		private var _box : MovingContainerBox;

		public function ProjectScroller(xml:XMLList, boxHeight:int, boxWidth:int) {
			_xml = xml;
			init(boxHeight,boxWidth);
		}
		
		private function init(boxHeight:int, boxWidth:int) : void {
			_container = new Sprite();
			_projects = new Array();
			
			var foo:int = 0;
			var space:int = 25;
			var dummySpace:int = int(_xml.@dummyHeight);
			
			for (var i : int = 0; i < _xml.children().length(); i++) {
				var proItem : ProjectItem = new ProjectItem( _xml.child(i), boxWidth );
					
				if(i==0){
					var dummy1 : Sprite = ColorRectangle.create( 0xffffff,0,proItem.width,dummySpace);
					foo += dummy1.height + space;
					_container.addChild(dummy1);	
				}
			
				proItem.y = foo;
				proItem.updateBlur(10);
				foo += proItem.height + space;
				_projects.push(proItem);
				_container.addChild(proItem);
				
				if(i==_xml.children().length()-1){
					var dummy2 : Sprite = ColorRectangle.create( 0xffffff,0,proItem.width,dummySpace);
					dummy2.y = foo;
					_container.addChild(dummy2);	
				}
			}
			
			_box = new MovingContainerBox(_container, _container.width, boxHeight, 0x000000, true);
			addChild(_box);
		}
		
		public function open(delay:Number=0):void{
			for (var i : int = 0; i < _projects.length; i++) {
				var proItem : ProjectItem = _projects[i];
				proItem.addEventListener(ProjectItem.EVENT_ACTION, projectClick);
			}			
			_box.addEventListener(MovingContainerBox.EVENT_MOVEACTION,moving);
			_box.open(delay);
		}
		
		public function close(delay:Number=0):void{
			removeListeners();
			_box.close(delay);
		}
		
		private function projectClick(e:ClickEvent) : void {
			dispatchEvent( new ClickEvent( ProjectItem.EVENT_ACTION, e.value) );
		}

		private function moving(e:Event) : void {
			//do cool shiteeee. 	
			for (var i : int = 0; i < _projects.length; i++) {
				var proItem : ProjectItem = _projects[i];
				var distance : Number = Math.abs(_container.mouseY - proItem.y);
				var blurFactor : Number;
				if(distance>50) blurFactor = (distance/10)/2;
				else blurFactor = 0;
				proItem.updateBlur(blurFactor);				
			}		
		}
		
		public function resize(h:int) : void {
			_box.resize(h);
		}

		public function get contentWidth():int{
			return _container.width;
		}
		
		public function destroy() : void {
			removeListeners();		
			_box.destroy();
		}
		
		private function removeListeners() : void {
			_box.removeEventListener(MovingContainerBox.EVENT_MOVEACTION,moving);
			for (var i : int = 0; i < _projects.length; i++) {
				var proItem : ProjectItem = _projects[i];
				proItem.removeEventListener(ProjectItem.EVENT_ACTION, projectClick);
			}	
		}
	}
}
